﻿using System;
using System.Windows.Forms;

namespace Note_Assistant
{
    public partial class LoginForm : Form
    {
        // 简单的用户名和密码（实际应用中应该使用更安全的方式存储和验证）
        private const string ValidUsername = "admin";
        private const string ValidPassword = "password123";

        public LoginForm()
        {
            InitializeComponent();
            SetupLoginUI();
        }

        private void SetupLoginUI()
        {
            this.Text = "笔记助手 - 登录";
            this.Size = new Size(400, 300);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;

            var mainPanel = new Panel { Dock = DockStyle.Fill, Padding = new Padding(20) };

            var titleLabel = new Label
            {
                Text = "笔记助手登录",
                Dock = DockStyle.Top,
                TextAlign = ContentAlignment.MiddleCenter,
                Font = new Font("微软雅黑", 16, FontStyle.Bold),
                Height = 50
            };

            var usernameLabel = new Label
            {
                Text = "用户名:",
                Dock = DockStyle.Top,
                Margin = new Padding(0, 20, 0, 5)
            };

            var usernameTextBox = new TextBox
            {
                Dock = DockStyle.Top,
                Height = 30
            };

            var passwordLabel = new Label
            {
                Text = "密码:",
                Dock = DockStyle.Top,
                Margin = new Padding(0, 10, 0, 5)
            };

            var passwordTextBox = new TextBox
            {
                Dock = DockStyle.Top,
                Height = 30,
                PasswordChar = '*'
            };

            var loginButton = new Button
            {
                Text = "登录",
                Dock = DockStyle.Top,
                Height = 40,
                Margin = new Padding(0, 20, 0, 0),
                BackColor = SystemColors.Highlight,
                ForeColor = Color.White
            };

            var statusLabel = new Label
            {
                Dock = DockStyle.Bottom,
                Height = 30,
                TextAlign = ContentAlignment.MiddleCenter,
                ForeColor = Color.Red
            };

            loginButton.Click += (sender, e) =>
            {
                if (string.IsNullOrEmpty(usernameTextBox.Text))
                {
                    statusLabel.Text = "请输入用户名";
                    return;
                }

                if (string.IsNullOrEmpty(passwordTextBox.Text))
                {
                    statusLabel.Text = "请输入密码";
                    return;
                }

                if (usernameTextBox.Text == ValidUsername && passwordTextBox.Text == ValidPassword)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    statusLabel.Text = "用户名或密码错误";
                }
            };

            // 按Enter键也可以触发登录
            passwordTextBox.KeyDown += (sender, e) =>
            {
                if (e.KeyCode == Keys.Enter)
                {
                    loginButton.PerformClick();
                }
            };

            mainPanel.Controls.Add(loginButton);
            mainPanel.Controls.Add(passwordTextBox);
            mainPanel.Controls.Add(passwordLabel);
            mainPanel.Controls.Add(usernameTextBox);
            mainPanel.Controls.Add(usernameLabel);
            mainPanel.Controls.Add(titleLabel);
            mainPanel.Controls.Add(statusLabel);

            this.Controls.Add(mainPanel);
        }
    }
}
